package com.saver.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.saver.common.utils.R;
import com.saver.modules.sys.service.SysAppendixService;
import com.saver.office.register.OfficeRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

/**
 * 文件上传
 *
 * @author yuxiaolong
 * @email 1005471232@qq.com
 * @date 2018年08月30日 下午11:22
 */
@RestController
@RequestMapping("/sys/appendix")
public class SysAppendixController {

    @Autowired
    private SysAppendixService sysAppendixService;

    /**
     * 上传文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R upload(@RequestBody MultipartFile file) throws IOException {
        return sysAppendixService.uploadFile(file);
    }

    @PostMapping("/open")
    public R open(@RequestBody Map<String,Object> map){
        Integer id = (Integer) map.get("id");
        String uuid = (String) map.get("uuid");
        Boolean readOnly = (Boolean) map.get("readOnly");
        return sysAppendixService.open(id,uuid,readOnly);
    }

    /**
     * 下载上传的文件
     *
     * @param uuid
     * @param response
     * @throws IOException
     */
    @GetMapping("/download")
    public void download(@RequestParam("uuid") String uuid, HttpServletResponse response) throws IOException {
        sysAppendixService.download(uuid, response);
    }



    /**
     * 下载
     *
     * @param id
     * @param response
     * @throws IOException
     */
    @GetMapping("/downloadAppendix")
    public void downloadAppendix(@RequestParam("id") Integer id, HttpServletResponse response) throws IOException {
        sysAppendixService.downloadAppendix(id, response);
    }



    /**
     * 删除附件
     *
     * @param id
     * @return R
     */
    @PostMapping("/deleteAppendix")
    public R deleteAppendix(@RequestBody Integer id) {
        return sysAppendixService.deleteAppendix(id);
    }


}
